<template>
  <div class="box">
    <div class="searcharea">
      <!-- 搜索 -->
      <el-row>
        <el-col :span="24">
          <div class="query">
            <el-form :inline="true" :model="queryUserinfoParam" class="demo-form-inline">
              <el-form-item label="工号">
                <el-input v-model="queryUserinfoParam.JobNumber" placeholder="请输入" clearable style="width: 200px" />
              </el-form-item>

              <el-form-item label="用户名称">
                <el-input v-model="queryUserinfoParam.UserName" placeholder="请输入" clearable style="width: 200px" />
              </el-form-item>

              <el-form-item>
                <el-button type="primary" :icon="Search" @click="SearchUserinfo()">搜索</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
    </div>

    <div class="contentarea">
      <!-- 功能按钮 -->
      <el-row>
        <el-col :span="24">
          <div class="functionbutton">
            <el-button type="primary" plain @click="addUserinfo()">添加</el-button>
            <el-button type="danger" plain @click="batchDeletionUserinfo()">批量删除</el-button>
          </div>
        </el-col>

        <!-- 添加用户 -->
        <el-dialog v-model="centerDialogVisible" title="添加用户" width="30%" align-center>
          <el-form
            ref="CreateUserinfoForm"
            :model="CreateUserinfoForm"
            :rules="CreateUserinfoRules"
            label-width="65px"
            style="padding: 20px"
          >
            <el-form-item prop="UserName" label="姓名">
              <el-input v-model="CreateUserinfoForm.UserName" placeholder="请输入姓名"> </el-input>
            </el-form-item>
            <el-form-item prop="PhoneNo" label="手机号">
              <el-input v-model="CreateUserinfoForm.PhoneNo" placeholder="请输入手机号"> </el-input>
            </el-form-item>

            <el-form-item label="性别">
              <el-radio-group v-model="CreateUserinfoForm.Sex">
                <el-radio value="1">男</el-radio>
                <el-radio value="0">女</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="部门">
              <el-select v-model="CreateUserinfoForm.DepartmentId" placeholder="请选择" clearable>
                <el-option
                  v-for="option in DepartmentOptions"
                  :key="option.id"
                  :label="option.departmentName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="角色">
              <el-select v-model="CreateUserinfoForm.RoleinfoId" placeholder="请选择" clearable>
                <el-option
                  v-for="option in RoleinfoOptions"
                  :key="option.key"
                  :label="option.roleName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
          </el-form>

          <template #footer>
            <div class="dialog-footer">
              <el-button @click="centerDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="CreateUserinfoDialog()">确认</el-button>
            </div>
          </template>
        </el-dialog>
      </el-row>

      <!-- 数据表格 -->
      <el-row>
        <el-col :span="24">
          <div>
            <el-table
              :data="tableData"
              border
              style="width: 100%"
              max-height="400px"
              :selection="selectedRows"
              @selection-change="handleSelectionChange"
              @row-click="getRowkey"
              show-overflow-tooltip
              empty-text="暂无数据"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column type="index" label="序号" />
              <el-table-column prop="jobNumber" label="工号" width="180" />
              <el-table-column prop="userName" label="姓名" />
              <el-table-column prop="phoneNo" label="手机号" />
              <el-table-column prop="sex" label="性别">
                <template v-slot="scope">
                  {{ scope.row.sex === 1 ? '男' : '女' }}
                </template>
              </el-table-column>
              <el-table-column prop="department.departmentName" label="部门名称" />
              <el-table-column prop="roleinfo.roleName" label="角色" />
              <el-table-column prop="creator" label="创建人" />
              <el-table-column prop="updator" label="修改人" />
              <el-table-column prop="createtime" label="创建时间" width="180" :formatter="formatColumnTime" />
              <el-table-column prop="updateDate" label="更新时间" width="180" :formatter="formatColumnTime" />
              <el-table-column fixed="right" label="操作" width="140">
                <template #default="scope">
                  <el-button type="danger" plain size="small" @click="DeleteUserinfoSingle(scope.row.id)">
                    删除
                  </el-button>
                  <el-button type="primary" plain size="small" @click="UpdateUserinfo(scope.row)">更新</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-col>

        <el-dialog v-model="UpdateDialogVisible" title="修改用户" width="30%" align-center>
          <el-form
            ref="UpdateUserinfoForm"
            :model="UpdateUserinfoForm"
            :rules="CreateUserinfoRules"
            label-width="65px"
            style="padding: 20px"
          >
            <el-form-item prop="UserName" label="姓名">
              <el-input v-model="UpdateUserinfoForm.UserName" placeholder="请输入姓名"> </el-input>
            </el-form-item>
            <el-form-item prop="PhoneNo" label="手机号">
              <el-input v-model="UpdateUserinfoForm.PhoneNo" placeholder="请输入手机号"> </el-input>
            </el-form-item>

            <el-form-item label="性别">
              <el-radio-group v-model="UpdateUserinfoForm.Sex">
                <el-radio :value="1">男</el-radio>
                <el-radio :value="0">女</el-radio>
              </el-radio-group>
            </el-form-item>

            <el-form-item label="部门">
              <el-select v-model="UpdateUserinfoForm.DepartmentId" placeholder="请选择" clearable>
                <el-option
                  v-for="option in DepartmentOptions"
                  :key="option.kye"
                  :label="option.departmentName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="角色">
              <el-select v-model="UpdateUserinfoForm.RoleinfoId" placeholder="请选择" clearable>
                <el-option
                  v-for="option in RoleinfoOptions"
                  :key="option.key"
                  :label="option.roleName"
                  :value="option.id"
                />
              </el-select>
            </el-form-item>
          </el-form>

          <template #footer>
            <div class="dialog-footer">
              <el-button @click="UpdateDialogVisible = false">取消</el-button>
              <el-button type="primary" @click="UpdateUserinfoDialog()">确认</el-button>
            </div>
          </template>
        </el-dialog>
      </el-row>

      <!-- 分页 -->
      <el-row>
        <el-col :span="24">
          <div class="demo-pagination-block" style="display: flex; justify-content: flex-end">
            <el-config-provider :locale="zhCn">
              <el-pagination
                v-model:current-page="queryUserinfoParam.PageIndex"
                v-model:page-size="queryUserinfoParam.PageSize"
                :page-sizes="[100, 200, 300, 400]"
                :small="false"
                :disabled="false"
                :background="true"
                layout="total, sizes, prev, pager, next, jumper"
                :total="queryUserinfoParam.totalCount"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              >
              </el-pagination>
            </el-config-provider>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script >
import { Search } from '@element-plus/icons-vue'
import { ApiGetBasicData_DepartmentPageListAsync } from '@/api/basic/basicDataDepartmentApi'
import {
  ApiGetBasicData_UserinfoPageListAsync,
  ApiUpdateBasicData_UserinfoAsync,
  ApiBatchDeletionBasicData_UserinfoAsync,
  ApiCreateBasicData_UserinfoAsync
} from '@/api/basic/basicDataUserinfoApi'

import { ApiGetBasicData_RoleinfoPageListAsync } from '@/api/basic/basicDataRoleinfoApi'
import { formatTime, capitalizeFirstLetter } from '../../utils/index'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import { ElMessageBox, ElNotification } from 'element-plus'

export default {
  name: 'UserinfoView',
  data() {
    return {
      UpdateUserinfoForm: {},
      RoleinfoOptions: [],
      UpdateDialogVisible: false,
      DepartmentOptions: null,
      CreateUserinfoForm: {
        PhoneNo: null,
        UserName: null
      },
      CreateUserinfoRules: {
        PhoneNo: [{ required: true, trigger: 'blur', message: '手机号不能为空' }],
        UserName: [{ required: true, trigger: 'blur', message: '姓名不能为空' }]
      },

      centerDialogVisible: false,
      selectedRows: [], // 存储选中的行数据
      queryUserinfoParam: {
        JobNumber: null,
        UserName: null,
        PageIndex: 1,
        PageSize: 100,
        totalCount: 11
      },
      queryRoleinfoParam: {
        PageIndex: 1,
        PageSize: 100
      },
      queryDepartmenParam: {
        PageIndex: 1,
        PageSize: 100
      },
      tableData: [] // 表格数据
    }
  },
  setup() {
    return {
      Search,
      zhCn
    }
  },
  created() {
    this.SearchUserinfo()
  },
  methods: {
    //确认更新
    UpdateUserinfoDialog() {
      this.$refs.UpdateUserinfoForm.validate((valid) => {
        if (valid) {
          ApiUpdateBasicData_UserinfoAsync(this.UpdateUserinfoForm)
            .then((res) => {
              if (res.data.data) {
                ElNotification({
                  message: '更新成功',
                  offset: 100,
                  type: 'success',
                  duration: 2000
                })
                this.UpdateDialogVisible = false
                this.SearchUserinfo()
              }
            })
            .catch(() => {
              ElNotification({
                message: '更新失败',
                type: 'warning',
                offset: 100,
                duration: 2000
              })
            })
        }
      })
    },
    //更新用户
    UpdateUserinfo(res) {
      this.UpdateDialogVisible = true
      this.UpdateUserinfoForm = capitalizeFirstLetter(res)

      // 获取部门列表
      ApiGetBasicData_DepartmentPageListAsync(this.queryDepartmenParam)
        .then((res) => {
          this.DepartmentOptions = res.data.data
        })
        .catch()

      // 获取角色列表
      ApiGetBasicData_RoleinfoPageListAsync(this.queryRoleinfoParam)
        .then((res) => {
          this.RoleinfoOptions = res.data.data
        })
        .catch()
    },
    //删除用户
    DeleteUserinfoSingle(res) {
      this.selectedRows = []
      this.selectedRows.push(res)
      this.batchDeletionUserinfo()
    },
    //添加用户操作
    CreateUserinfoDialog() {
      this.$refs.CreateUserinfoForm.validate((valid) => {
        if (valid) {
          ApiCreateBasicData_UserinfoAsync(this.CreateUserinfoForm).then((res) => {
            if (res.data.data) {
              ElNotification({
                message: '添加用户成功',
                offset: 100,
                type: 'success',
                duration: 2000
              })
              this.centerDialogVisible = false
              this.SearchUserinfo()
            }
          })
        }
      })
    },

    //获取用户选择项
    handleSelectionChange(select) {
      // 提取传递过来的对象中的"id"字段，并存储到this.selectedRows中
      this.selectedRows = select.map((item) => item.id)
    },
    //添加用户
    addUserinfo() {
      this.centerDialogVisible = true

      // 获取部门列表
      ApiGetBasicData_DepartmentPageListAsync(this.queryDepartmenParam)
        .then((res) => {
          this.DepartmentOptions = res.data.data
        })
        .catch(() => {
          console.log()
        })

      // 获取角色列表
      ApiGetBasicData_RoleinfoPageListAsync(this.queryRoleinfoParam)
        .then((res) => {
          this.RoleinfoOptions = res.data.data
        })
        .catch()
    },
    //指删除用户
    batchDeletionUserinfo() {
      if (this.selectedRows.length < 1) {
        ElNotification({
          message: '请先选择一条或更多信息',
          type: 'warning',
          offset: 100,
          duration: 2000
        })
        return
      }
      ElMessageBox.confirm('确实要删除这些信息吗?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          ApiBatchDeletionBasicData_UserinfoAsync(this.selectedRows).then((res) => {
            if (res.data.data) {
              ElNotification({
                message: '删除成功',
                offset: 100,
                type: 'success',
                duration: 2000
              })
              this.SearchUserinfo()
            }
          })
        })
        .catch(() => {
          ElNotification({
            type: 'info',
            message: 'Delete canceled'
          })
        })
    },
    //时间转换
    formatColumnTime(row, column, time) {
      return formatTime(time)
    },

    //每页条数
    handleSizeChange(e) {
      this.queryUserinfoParam.PageSize = e
      this.SearchUserinfo()
    },

    // 搜索
    SearchUserinfo() {
      ApiGetBasicData_UserinfoPageListAsync(this.queryUserinfoParam).then((res) => {
        this.tableData = res.data.data
        this.queryUserinfoParam.PageIndex = res.data.pageIndex
        this.queryUserinfoParam.PageSize = res.data.pageSize
        this.queryUserinfoParam.totalCount = res.data.totalCount
      })
    }
  }
}
</script>

<style lang="less"  scoped>
@import '../../assets/globle/index.css';
</style>